package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> getpage(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<Item> page1 = itemService.page(new Page<>(page, size));

        return new PageDTO<>(page1.getTotal(),page1.getRecords());
    }

    /**
     * 根据id查
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable("id") Long id){
        return itemService.getById(id);
    }

    /**
     * 添加商品
     * @param item
     */
    @PostMapping()
    public void add(@RequestParam Item item){
        itemService.save(item);
    }

    /**
     * 商品上架下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void statu(@PathVariable("id") Long id,@PathVariable("status") int status){
        itemService.updateStatus(id,status);
    }

    @PutMapping
    public void update(@RequestBody Item item){
        itemService.updateById(item);
    }

    @DeleteMapping("/{id}")
    public void delectByid(@PathVariable("id") long id){
        itemService.removeById(id);
    }
}
